<template>
    <view class="return-cart-bar">
        <!-- <view class="bottom-bar">
			<view class="flex-s-c" v-if='supplierStatus==1||supplierStatus==2'>
				<view class="flex-v-c ">
					<view class="cart-box" @click="cartShow">
						<image src="/static/img/box.png" mode="" class="image"></image>
						<text class="cart-num">{{supplierCartData.num}}</text>
					</view>
					<text class="total-price"><text class="price-unit">￥</text>{{supplierCartData.totalPrice}}</text>
				</view>
				<text class="btn btn-success" @click="gotoSubmitOrder">去结算</text>
			</view>
			<text v-if='supplierStatus==-1' class="btn btn-success" @click="apply(supplierStatus)">申请报价</text>
			<text v-if='supplierStatus==0' class="btn btn-success" @click="apply(supplierStatus)">审核中</text>
		</view> -->
        <view class="bottom-bar">
            <view class="flex-s-c" v-if="supplierStatus.OfferStatus == 2">
                <view class="flex-v-c">
                    <view class="cart-box" @click="cartShow">
                        <image src="/static/img/box.png" mode="" class="image"></image>
                        <text class="cart-num">{{ supplierCartData.num }}</text>
                    </view>
                    <text class="total-price"><text class="price-unit">￥</text>{{ supplierCartData.totalPrice }}</text>
                </view>
                <text class="btn btn-success" @click="gotoSubmitOrder">去结算</text>
            </view>
            <view class="first" v-if="supplierStatus.Status == 3 && ApplySupplierPirce() == 1"
                >申请报价未通过，如有疑问联系店铺</view
            >
            <text
                v-if="supplierStatus.OfferStatus == 0 && supplierStatus.Status == -1"
                class="btn btn-success"
                @click="apply(supplierStatus.Status)"
                >申请报价</text
            >
            <text
                v-if="supplierStatus.OfferStatus == 1 && supplierStatus.Status == 0"
                class="btn btn-success-hollow"
                @click="apply(supplierStatus.Status)"
                >待授权</text
            >
            <text
                v-if="supplierStatus.OfferStatus == 1 && supplierStatus.Status == 1"
                class="btn btn-success-hollow"
                @click="apply(supplierStatus.Status)"
                >待审核</text
            >
        </view>
        <uni-popup type="bottom" ref="cart" @change="close" :safe-area="false">
            <view class="popup-content">
                <view class="popup-title">
                    <text class="popup-title-text">已选商品</text>
                    <text class="popup-title-action" @click="closePopup">清空</text>
                </view>
                <scroll-view scroll-y="true" class="scroll">
                    <view
                        class="cart-item"
                        v-for="(cartItem, index) in supplierCartData.list"
                        :key="cartItem.ProductId">
                        <cart-item-product :productData="cartItem"></cart-item-product>
                        <cart-item-specs
                            :spaceItem="spaceItem"
                            v-for="(spaceItem, spaceIndex) in cartItem.SubItems"
                            :key="spaceItem.ID"></cart-item-specs>
                    </view>
                </scroll-view>
            </view>
        </uni-popup>
    </view>
</template>

<script>
import cartItemProduct from "@/pages_supplier/components/supplier/bottomCart/cart-item-product.vue"
import cartItemSpecs from "@/pages_supplier/components/supplier/bottomCart/cart-item-specs.vue"
import {
    GetSupplier,
    GetSupplierTypeTreeList,
    GetSupplierOffer,
    GetProductDetail,
    ApplySupplierOffter,
} from "@/service/api/supplier.js"
import { eventBus } from "@/utils/eventBus.js"
import { getMarkSupplierCarList, deleteSupplierCar } from "@/utils/cartUtil.js"
export default {
    name: "supplierBottomCart",
    components: {
        cartItemProduct,
        cartItemSpecs,
    },
    props: {
        tempType: {
            type: Number,
            default: 0,
        },
        supplierStatus: {
            type: Object,
            default: () => {},
        },
        supplierId: {
            type: Number,
            default: 0,
        },
    },
    data() {
        return {
            cartOpen: false,
            cartList: [],
            total: {
                totalNum: 0,
                totalPrice: 0,
                class: 0,
            },
            supplierCartData: {},
            refresh: false,
        }
    },
    mounted() {
        this.upMarkSupplier()
        eventBus.$on("SET_CART", (msg) => {
            this.upMarkSupplier()
        })
    },
    methods: {
        //更新购物袋
        upMarkSupplier() {
            getMarkSupplierCarList(this.supplierId).then((data) => {
                this.supplierCartData = data
                this.$forceUpdate()
            })
        },
        cartShow() {
            this.cartOpen = !this.cartOpen
            if (this.cartOpen) {
                this.$refs.cart.open()
            } else {
                this.$refs.cart.close()
            }
        },
        close(e) {
            if (e.show == false) {
                this.cartOpen = false
            }
        },
        closePopup() {
            if (!this.supplierCartData.ids) {
                this.$refs.cart.close()
                return
            }
            let param = {
                ids: this.supplierCartData.ids,
            }
            this.doDelCart(param)
        },
        //删除接口
        doDelCart(param) {
            uni.showLoading({
                title: "提交中",
            })
            deleteSupplierCar(param).then((res) => {
                if (res) {
                    uni.hideLoading()
                    this.$refs.cart.close()
                }
            })
        },
        // 去结算
        gotoSubmitOrder() {
            let ids = ""
            if (this.supplierCartData && this.supplierCartData.ids && this.supplierCartData.ids != "") {
                ids = this.supplierCartData.ids
                if (ids.charAt(ids.length - 1) == ",") {
                    ids = ids.slice(0, -1)
                }
            } else {
                this.easyToast("当前购物车为空，请添加商品")
                return false
            }
            this.easyGoTo("/pages_supplier/packet/order/submitOrder?ids=" + ids)
        },
        // 申请供应商
        apply(status) {
            if (status == 0) {
                this.easyToast("正在审核中，请耐心等待")
            } else if (status == -1) {
                ApplySupplierOffter(this.supplierId).then((res) => {
                    if (res) {
                        this.easyToast("申请已提交，等待审核")
                        this.GetSupplierOffer(this.supplierId)
                    }
                })
            }
        },
        buyNow() {
            this.$emit("buyNow", "")
        },
    },
}
</script>

<style lang="scss" scoped>
.return-cart-bar {
    position: relative;
    z-index: 1;
}

.bottom-bar {
    z-index: 100;
    justify-content: flex-end;
    padding: 0 $app-page-padding;

    .flex-s-c {
        flex: 1;
        display: flex;
        justify-content: space-between;
    }

    .cart-box {
        position: relative;
        margin-right: 30rpx;

        .image {
            width: 80rpx;
            height: 80rpx;
        }

        .cart-num {
            position: absolute;
            top: -8rpx;
            right: -10rpx;
            background: #fe5128;
            color: $color-white;
            padding: 0 10rpx;
            line-height: 1.5;
            border-radius: 18rpx;
        }
    }

    .total-price {
        font-size: 40rpx;
        font-weight: bold;
    }

    .price-unit {
        font-size: 28rpx;
        font-weight: bold;
    }
}

// 弹窗组件
.popup-content {
    width: auto;
    display: flex;
    flex-direction: column;
    position: relative;
    background: #f5f5f5;
    border-radius: 40rpx 40rpx 0 0;
    padding-right: $app-page-padding;
    padding-left: $app-page-padding;
    padding-bottom: 120rpx;
    z-index: 0;

    .popup-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 100rpx;

        .popup-title-text {
        }

        .popup-title-action {
        }
    }

    .scroll {
        max-height: 700rpx;

        .cart-item {
            background: $color-white;
            padding: 0 $app-page-padding;
            border-radius: $border-radius-10;
            margin-bottom: 20rpx;
        }
    }
}

.cart-left {
    display: flex;
    flex-direction: row;
    align-items: center;
}

.cart-kind {
    display: flex;
    flex-direction: column;
    margin-left: $app-page-padding;

    .cart-price {
        font-size: $font-size-24;

        text {
            font-size: $font-size-36;
            font-weight: 700;
        }
    }

    .kind {
        color: $color-orange-1;
    }
}
</style>
